package cn.edu.sgu.www.mhxysy.controller.system;

import cn.edu.sgu.www.mhxysy.annotation.AnonymityAccess;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.system.UserLoginDTO;
import cn.edu.sgu.www.mhxysy.dto.system.UserPassUpdateDTO;
import cn.edu.sgu.www.mhxysy.easyui.component.impl.PropertyGrid;
import cn.edu.sgu.www.mhxysy.entity.system.User;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.system.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "用户管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@RestController
@RequestMapping(path = "/user", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class UserController {

	private final UserService userService;

	@Autowired
	public UserController(UserService userService) {
		this.userService = userService;
	}

	@ApiOperation("退出登录")
	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	public JsonResult<Void> logout() {
		userService.logout();

		return JsonResult.success("退出成功");
	}

	@AnonymityAccess
	@ApiOperation("登录认证")
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public JsonResult<Void> login(@Validated UserLoginDTO loginDTO) {
		userService.login(loginDTO);

		return JsonResult.success("登录成功");
	}

	@ApiOperation("修改用户信息")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResult<Void> update(User user) {
		userService.update(user);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("修改用户的密码")
	@RequestMapping(value = "/updatePass", method = RequestMethod.POST)
	public JsonResult<Void> updatePass(@Validated UserPassUpdateDTO userUpdateDTO) {
		userService.updatePass(userUpdateDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("获取登录的用户信息")
	@RequestMapping(value = "/getLogin", method = RequestMethod.GET)
	public JsonResult<User> getLogin() {
		User user = userService.getLogin();

		return JsonResult.success(null, user);
	}

	@ApiOperation("查询用户详情")
	@RequestMapping(value = "/selectUserDetails", method = RequestMethod.GET)
	public JsonResult<List<PropertyGrid>> selectUserDetails() {
		List<PropertyGrid> list = userService.selectUserDetails();

		return JsonResult.success(null, list);
	}

}